Reader/writer lock with reduced cache contention

ABSTRACT

A scalable locking system is described herein that allows processors to access shared data with reduced cache contention to increase parallelism and scalability. The system provides a reader/writer lock implementation that uses randomization and spends extra space to spread possible contention over multiple cache lines. The system avoids updates to a single shared location in acquiring/releasing a read lock by spreading the lock count over multiple sub-counts in multiple cache lines, and hashing thread identifiers to those cache lines. Carefully crafted invariants allow the use of partially lock-free code in the common path of acquisition and release of a read lock. A careful protocol allows the system to reuse space allocated for a read lock for subsequent locking to avoid frequent reallocating of read lock data structures. The system also provides fairness for write-locking threads and uses object pooling techniques to make reduce costs associated with the lock data structures.

BACKGROUND

In multi-threaded programs running on multiprocessors, different threadsmay attempt to access shared data structures concurrently. Such accessis generally coordinated via some form of locking: all threads acquire alock before accessing the data structure (and hold the lock for theduration of the access). The simplest form of locking is mutualexclusion—only one thread at a time can hold the lock, so only a singlethread is accessing the data structure at a time. However, in many casesone can classify the operations performed on the data structure asreaders and/or writers; reader operations only read the data structure,but writer operations may update it. Several reader operations maysafely run concurrently, but only one writer operation may safely run ata time. Therefore, it is useful to reflect this in the lockingprimitives, and the concurrent programming literature has long had theconcept of reader/writer locks. Such a lock can either be acquired inread (or shared) mode, or write (or exclusive) mode—several threads mayacquire the lock in read mode, but only one thread may acquire the lockin write mode. Thus, a reader/writer lock can protect a shared datastructure, and when operations on the data structure have beenclassified as reader or writer operations, they can acquire the lock inthe corresponding mode. For many shared data structures, it is commonfor reader operations to be much more frequent than writer operations,so that reader/writer locks allow significantly greater parallelscalability.

It is common for computers today to have many more processing cores thancomputers of just a few years ago. Where once computers with more than2-4 cores were only found in database servers or supercomputers, evendesktop computer systems can be ordered today with eight or moreprocessor cores. The increased number of processors increases thesharing of resources such as memory, and exacerbates inefficient use ofsuch resources, including cache faults caused by multiple processorsmodifying the same data.

Unfortunately, the most common implementations or reader/writer locksinclude a single variable that tracks the number of readers, andsometimes a separate variable that tracks writers. With many readersexecuting code on different processors, the reader/writer lock itselfcan quickly become a source of cache contention. For example, one commonstrategy in reader/writer lock implementations is to maintain a variablerepresenting the number of threads that have acquired the lock in readmode, updating this count variable with atomic hardware instructions.This causes cache contention, as each processor that updates the countacquires the cache line containing the count variable in exclusive mode.As the number of processors in a machine increases, contention on evenone cache line can severely limit performance. The impact of contentionis determined partly by the rate at which processors access thecontended cache line. If N processors each execute a loop in which theyacquire a read lock to execute a read operation, the rate at which theread lock is acquired, and thus the shared cache line is accessed, willdepend on the duration of the operation executed within the lock—theshorter the operation, the greater the contention. Thus, whereas forexclusive locks software developers generally increase parallelism bydoing less inside locks, for reader/writer locks developers oftenreceive paradoxical guidance, recommending increases in parallelism bydoing more inside locks.

SUMMARY

A scalable locking system is described herein that allows processors ina multiprocessor system to access shared data with reduced cachecontention to increase scalability. The system provides a reader/writerlock implementation that reduces contention in scenarios in which ashared data structure is accessed in a read-only manner. The scalablelock system uses randomization and spends extra space to spread possiblecontention over multiple cache lines. Instead of a single variable fortracking the number of readers, the system uses multiple lock countsspread over multiple cache lines. Carefully crafted invariants allow theuse of partially lock-free code in the common path of acquisition andrelease of a read lock. A careful protocol allows the system to reusespace allocated for a read lock for subsequent locking to avoid frequentreallocating of read lock data structures. The system avoids updates toa single shared location in acquiring/releasing a read lock by spreadingthe lock count over multiple sub-counts in multiple cache lines, andhashing thread identifiers to those cache lines. The system alsoprovides fairness for write-locking threads: a thread wishing to acquirea write lock cannot be “starved” for arbitrary periods by read-lockingthreads. In addition, the system uses object pooling techniques to makethe space/allocation/initialization costs associated with the lock datastructures acceptable. Thus, the system provides a more highly scalablereader/writer lock, particularly for the level of parallelism providedby modern multi-processor computer systems.

This Summary is provided to introduce a selection of concepts in asimplified form that are further described below in the DetailedDescription. This Summary is not intended to identify key features oressential features of the claimed subject matter, nor is it intended tobe used to limit the scope of the claimed subject matter.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 is a block diagram that illustrates components of the scalablelocking system, in one embodiment.

FIG. 2 is a block diagram that illustrates various data structuresallocated by the scalable lock system when the system is in use, in oneembodiment.

FIG. 3 is a flow diagram that illustrates processing of the system toacquire a read lock, in one embodiment.

FIG. 4 is a flow diagram that illustrates processing of the system toadd a new reader, in one embodiment.

FIG. 5 is a flow diagram that illustrates processing of the system toacquire a write lock, in one embodiment.

FIG. 6 is a flow diagram that illustrates processing of the system torelease a read or write lock, in one embodiment.

FIG. 7 is a flow diagram that illustrates processing of the system torelease a reader, in one embodiment.

DETAILED DESCRIPTION

A scalable locking system is described herein that allows manyprocessors in a multiprocessor system to access shared data with reducedcache contention to increase scalability. The system provides areader/writer lock implementation that reduces contention in scenariosin which a shared data structure is accessed in a read-only manner (ornearly so). Using the techniques described herein, frequent readeroperations performed on a shared data structure by multiple threadsreceive increased scalability, without requiring programmers to worryabout the size of the operations. The scalable lock system usesrandomization and spends extra space to spread possible contention overmultiple cache lines. Instead of a single variable for tracking thenumber of readers, the system uses multiple lock counts spread overmultiple cache lines. For example, the system may use a lock count perprocessor or other dividing scheme. Carefully crafted invariants allowthe use of partially lock-free code in the common path of acquisitionand release of a read lock. A careful protocol allows the system toreuse space allocated for a read lock for subsequent locking to avoidfrequent reallocating of read lock data structures. For example, thesystem may optimistically assume that the next request will be a readlock request after the last read lock has been released.

The system has the following attributes, in some embodiments. First, thesystem avoids updates to a single shared location in acquiring/releasinga read lock by spreading the lock count over multiple sub-counts inmultiple cache lines, and hashing thread identifiers to those cachelines. Second, the system provides fairness for write-locking threads: athread wishing to acquire a write lock cannot be “starved” for arbitraryperiods by read-locking threads. The system does this by providing alocking protocol through which a write-locking thread can preventfurther read-lock acquisition, thus inserting itself “in line” toacquire the write lock when the current read-lock holders release thelock. Third, the system uses object pooling techniques to make thespace/allocation/initialization costs associated with the lock datastructures acceptable. Thus, the system provides a more highly scalablereader/writer lock, particularly for the level of parallelism providedby modern multi-processor computer systems.

The system implements the reader/writer lock as a queue of “nodes,” eachrepresenting the interest in one or more threads in acquiring the lock.A “writer” node represents one thread attempting to acquire the lock inwrite (exclusive) mode, a “reader” node one or more threads attemptingto acquire the lock in read (shared) mode. The lock object containspointers to the head and tail of this queue, and the nodes containpointers to the next node in the queue. The thread or threads at thehead of the queue currently hold the lock; new nodes are inserted afterthe current tail.

The major difference with previous work is in the representation of areader node. Let N be a number equal to (or proportional to) the numberof processors on the machine. A reader node contains an array of size N,each of whose elements is initialized to point to a structure containinga lock count. This structure is lockable and contains sufficient paddingbytes to ensure that multiple such structures allocated sequentiallyhave lock and count fields on different cache lines. Abstractly, theread lock is held if the head node of the queue is a reader node, andthe sum of the count fields of the structures within the reader node isnon-zero. To obtain the read lock, a thread uses its hash code to choosea structure from the array, and obtains the lock on that object. It thenchecks the “next” field of the reader node—if this is non-null, thenanother thread wishes to obtain a write lock, and the reader threadenqueues after that writer. However, if it is null, then the thread canacquire the read lock by incrementing the count field of the chosenstructure.

When a write locking thread successfully enqueues a writer node after areader node, it has closed off further read locking via that node. Italso counts the number of non-zero counts in the structures in thearray, and waits for its node to become the head. The last readerthread, the one that decreases the aggregate lock count to zero, wakesup the waiting writer thread.

FIG. 1 is a block diagram that illustrates components of the scalablelocking system, in one embodiment. The system 100 includes an interfacecomponent 110, a queue component 120, a reader state component 130, ablocking component 140, a queue lock component 150, and an allocationcomponent 160. Each of these components is described in further detailherein.

The interface component 110 provides an interface to application codeand receives requests to acquire and release locks in both a read modeand a write mode. The interface component 110 may provide a programmaticinterface to applications through Microsoft .NET, the MicrosoftComponent Object Model (COM), or other common interface paradigms. Theinterface component 110 provides a class or other interface with one ormore functions or application programming interfaces (APIs) that anapplication can invoke to use the facilities of the system 100 describedherein.

The queue component 120 maintains a list of reader and writer nodes thatrepresent requests to acquire a lock and any current holder of the lock.For example, the head of the list may currently hold the lock and eachsubsequent node in the list may represent a next in line request toreceive the lock when the current holder of the lock releases the lock.Because the scalable lock system 100 uses a queue-based lockingapproach, the system 100 provides a lock that is inherently fair.Fairness in locking terminology means that one type of request (e.g.,readers) will not starve or prevent another type of request (e.g.,writers) from acquiring the lock. The queue component 120 ensures thatrequestors will acquire the lock in roughly the order that they requestthe lock. The queue component 120 also responds to requests within thesystem 100 to inspect the current head or tail of the list, add newreader or writer nodes to the list (e.g., upon a new acquire request),remove nodes from the list (e.g., upon a release request), and so forth.

The reader state component 130 maintains an indexed list of reader lockcounts for one or more reader nodes in the list maintained by the queuecomponent 120. The indexed list is structured so that lock counts aredistributed with enough space (e.g., padding) between each count so thataccessing a lock count at one index location is associated with adifferent cache line than accessing a lock count at any other indexlocation. This prevents cache contention when, for example, a threadrunning on one processor modifies a lock count because of acquiring orreleasing a read lock. The reader state component 130 may distribute thelock counts according to many different schemes, such as by creating onelock count per processor, creating a number of lock counts based on anumber of threads using the lock, or other variations that appropriatelyreduce cache contention for a particular application of the scalablelock system 100.

The blocking component 140 allows threads to block waiting for the lockefficiently and wakes threads when a particular thread has acquired thelock. The blocking component 140 may provide some amount of spinning(e.g., testing a lock value in a loop to optimistically acquire the lockwithout blocking) before putting a thread to sleep to be woken when thelock is available. Too much spinning can waste processor resources,while too little spinning can lead to unnecessary delay in acquiring thelock. The block component 140 may allow applications or administratorsto tune the amount of spinning, such as by setting a threshold number ofspin loop iterations. When the lock is released by a current lockholder, the blocking component 140 wakes a thread of the next node inthe list maintained by the queue component 120, indicating that thethread has the lock.

The queue lock component 150 is a lock that protects internal datamembers of the system 100. The queue lock component 150 may be a singlelock or may include several layers of granularity of locks within thedata structures of the system 100. For example, a main lock datastructure may have a lock for protecting its data members along withindividual data structures associated with indices within the indexedlist maintained by the reader state component 130. The system 100 avoidslocking internal members, particular in common code paths that aredesigned to reduce cache contention and promote a high degree ofparallelism. However, in some cases internal locking maintains theintegrity of lock data members potentially being accessed by manythreads at once.

The node allocation component 160 allocates new reader and writer nodesfor inclusion on the list maintained by the queue component 120. Thenode allocation component 160 may implement common techniques in the artto provide efficient creation and destruction of nodes. For example, thecomponent 160 may provide an object pool of reader and/or writer nodesthat other components can quickly reuse as the system 100 receives newlock requests.

The computing device on which the scalable locking system is implementedmay include a central processing unit, memory, input devices (e.g.,keyboard and pointing devices), output devices (e.g., display devices),and storage devices (e.g., disk drives or other non-volatile storagemedia). The memory and storage devices are computer-readable storagemedia that may be encoded with computer-executable instructions (e.g.,software) that implement or enable the system. In addition, the datastructures and message structures may be stored or transmitted via adata transmission medium, such as a signal on a communication link.Various communication links may be used, such as the Internet, a localarea network, a wide area network, a point-to-point dial-up connection,a cell phone network, and so on.

Note that as used herein, the term processor refers to a particularexecution unit, typically with an associated cache. Modern trends aremoving toward including many processor cores on a single processor chip.Each of these cores represents a processor as described herein, and thescalable lock system may provide a lock count associated with eachprocessor core. Thus, when referencing situations with multipleprocessors herein, these situations include a single processor chip withmultiple cores, multiple processor chips (each potentially havingmultiple processor cores), and so forth. In addition, other processingunits such as a graphical processing unit (GPU) having one or more coresor operating in conjunction with a central processing unit (CPU) may beused with the facilities of the system described herein to providereduced contention locking of data.

Embodiments of the system may be implemented in various operatingenvironments that include personal computers, server computers, handheldor laptop devices, multiprocessor systems, microprocessor-based systems,programmable consumer electronics, digital cameras, network PCs,minicomputers, mainframe computers, distributed computing environmentsthat include any of the above systems or devices, and so on. Thecomputer systems may be cell phones, personal digital assistants, smartphones, personal computers, programmable consumer electronics, digitalcameras, and so on.

The system may be described in the general context ofcomputer-executable instructions, such as program modules, executed byone or more computers or other devices. Generally, program modulesinclude routines, programs, objects, components, data structures, and soon that perform particular tasks or implement particular abstract datatypes. Typically, the functionality of the program modules may becombined or distributed as desired in various embodiments.

FIG. 2 is a block diagram that illustrates various data structuresallocated by the scalable lock system when the system is in use, in oneembodiment. The diagram includes a main lock data structure 210 thatincludes a head pointer 220 and a tail pointer 230 that point to thehead and tail respectively of the node list 240 maintained by the queuecomponent. The head of the node list typically holds the lock (exceptfor an optimization related to a last reader node where the lock is notcurrently held, as described further herein), and subsequent nodesrepresent readers and/or writers added to the list in an order in whicheach requested the lock. As shown, the node list 240 includes a writernode 250 at the head of the list followed by a reader node 260. Whilethe writer node 250 represents a single writer, the reader node 260 mayrepresent one or multiple readers. Note that a reader node at the headof the queue may also represent zero readers if no one holds the lockand the reader node remains on the queue waiting for the next reader torequest the lock. This increases performance by avoiding deallocation ofan unused reader node only to reallocate it in the common case of asubsequent request for the read lock. The reader node 260 references aseries of indexed counts 270 the total of which represents the number ofreaders currently holding the lock (or requesting the lock when thereader node 260 is not at the head of the node list 240). The indexedcounts 270 each comprise a data structure that includes a count 280 andpadding 290. The padding separates each count 280 so that each count isin or is likely to be in a separate cache line of one or moreprocessors. Many previous read/write lock implementations rely on asingle reader count on which each processor in a multi-processor systemcontends, often rendering the lock less useful than intended. Byproviding multiple lock counts, potentially one or more per processor,the scalable lock system reduces the amount of contention and allowsmore efficient use of the lock in the common case of many more readersthan writers acquiring the lock.

The diagram also shows that in a read-only workload, the queue willinclude only a single reader node. Threads will add themselves as readlockers by choosing an index into the array of indexed counts, and thenincrement the lock count of the chosen element. If the number ofelements is on the order the number of processors on the machine, andthe threads spread the accesses equally over the array elements,contention will be rare (and can be controlled by controlling the ratioof array size to number of processors). The total number of read lockersis the sum of the lock counts of all the indexed counts. When this isnon-zero for a reader node at the head of the queue, the lock is held inread mode by at least one thread.

FIG. 3 is a flow diagram that illustrates processing of the system toacquire a read lock, in one embodiment. Beginning in block 310, thesystem receives a request to acquire a read lock. For example, anapplication may call a function provided by the system for acquiring thelock in read mode. Continuing in block 320, the system inspects thequeue of waiting reader and writer nodes to identify the current queuetail. For example, the system may access a tail pointer of a main lockdata structure and dereference the pointer to access a node datastructure at the tail of the queue. Continuing in decision block 330, ifthe tail node of the queue is a reader node, then the system continuesat block 340, else the system continues at block 360.

Continuing in block 340 for a reader node, the system attempts toincrement a reader count, as described further with reference to FIG. 4.For example, the system may acquire a queue lock and attempt tore-access the queue to update an indexed lock count. Continuing indecision block 350, if the attempt to increment the reader count failed,then the system loops to block 320 to try again, else the systemcontinues at block 390. The increment attempt may fail, for example, ifthe system receives a request from a writer to acquire the lock beforethe reader can acquire the lock.

Continuing in block 360 for a writer node, the system locks the queue sothat the system can safely read and update data members associated withthe queue. For example, the system may use a data structure lockprovided by the programming language (e.g., a Microsoft .NET monitorlock) or other lock for protecting internal data members of theread/write lock. Continuing in block 370, the system adds a new readernode representing the current request to acquire a read lock to the tailof the queue. For example, the system may create the new reader node byallocating it or by removing it from an object pool. Continuing in block380, the system unlocks the queue so that other waiting threads canmodify the queue. For example, the system may drop the lock acquired inblock 360, or if using a monitor lock may simply exit the scope in whichthe lock was acquired.

Continuing in block 390, the system waits for the reader node to becomethe head of the queue. For example, the system may wait by spinning andif spinning fails to acquire the lock in a threshold period or number ofloop iterations, the system may block waiting for a prior lock holder toawaken the thread when the prior lock holder releases the lock. Thereader node becomes the head of the queue when all prior nodes in thequeue complete acquiring and subsequently releasing the lock. Afterblock 390, these steps conclude.

FIG. 4 is a flow diagram that illustrates processing of the system toadd a new reader, in one embodiment. Beginning in block 410, the systemdetermines a distribution index associated with a current thread. Thesystem attempts to distribute which of multiple lock counts each threadincrements to reduce cache contention produced by having a single lockcount. The thread identifier of each thread provides a semi-random bitof information that the system may use, based on a hash algorithm orother distribution scheme common in the art, to select an index amongmultiple available indices within an array of lock counts.

Continuing in block 420, the system locks the queue so that the systemcan safely read and update data members associated with the queue.Unlike many previous read/write locks, locking the queue of the presentsystem includes locking one of multiple lock counts to read (i.e., not asingle shared lock count) and locking each of the counts to write. Inthis way, the same “distribution index” determines the lock that isacquired that determines which sub-counter the system increments.Distributing the lock obtained reduces cache contention among multiplelogical processors. Continuing in decision block 430, if there is awriter node subsequent to the current reader node, then the systemcontinues at block 440, else the system continues at block 450. Asubsequent writer node indicates that the system has received a requestto acquire the write lock. To ensure that readers do not starve writerrequests (i.e., fairness), the system prevents further reader requestson the same reader node, and fails the request to increment the count.Continuing in block 440, the system sets the result of the readerincrement operation to failed, and then continues in block 460.

Turning to block 450, the system increments the indexed reader countbased on the determined distribution index. The indexed reader count isin a cache line isolated from other reader counts (as is the lock thatprotects the reader count) to reduce cache contention produced by theact of updating the reader count. Otherwise, the next time anotherprocessor read or attempted to update the reader count, the processorwould experience a cache fault to load the change made by the currentprocessor. By distributing the reader counts, the system trades space toreduce contention and increase execution efficiency. Continuing in block460, the system unlocks the queue so that other waiting threads canmodify the queue. After block 460, these steps conclude.

FIG. 5 is a flow diagram that illustrates processing of the system toacquire a write lock, in one embodiment. Beginning in block 510, thesystem receives a request to acquire a write lock. For example, anapplication may call a function provided by the system for acquiring thelock in write mode. Continuing in block 520, the system creates a newwriter node. For example, the system may allocate the new writer nodeusing an allocation function or may retrieve the writer node from anobject pool of reusable writer node objects.

Continuing in block 530, the system locks the queue so that the systemcan safely read and update data members associated with the queue.Continuing in block 540, the system inspects the queue of waiting readerand writer nodes to identify the current queue tail. For example, thesystem may access a tail pointer of a main lock data structure anddereference the pointer to access a node data structure at the tail ofthe queue. Continuing in decision block 550, if the tail node of thequeue is a reader node, then the system continues at block 560, else thesystem jumps to block 580. Continuing in block 560, the system checks acount of readers at each of multiple indices within the reader node atthe tail of the queue. Because of the way the system distributes lockrequests received from readers to lock count variables in multiple cachelines to reduce cache contention, the total of the lock count variablesindicates the actual number of readers. To obtain the write lock, thethread acquiring the lock locks all of the locks protecting the readcounts in order to install itself as the reader node's successor in thequeue. Continuing in decision block 570, if any readers hold the lock,then the system continues at block 573, else the system continues atblock 576. If any of the index counts is non-zero, then at least onereader holds the lock.

Continuing in block 573, the system sets a countdown associated with thereader node to a number of remaining non-zero read counts associatedwith the reader node holding the lock. Turning to block 576, if the testof block 570 found no remaining readers holding the lock, then thesystem removes the reader node from the queue. The system may leave areader node on the queue even though no readers currently hold the lockfor the optimistic case that a next request is a request to acquire aread lock, in which case the existing reader node structure can be usedwithout the added cost of tearing it down at the previous release andsetting it back up at the next acquire request. When the next request isa write request, the system may remove the reader node at that time.

Continuing in block 580, the system adds the new writer noderepresenting the current request to acquire a write lock to the tail ofthe queue. Continuing in block 5850, the system unlocks the queue sothat other waiting threads can modify the queue. For example, the systemmay drop the lock acquired in block 530, or if using a monitor lock maysimply exit the scope in which the lock was acquired to release the lockimplicitly. Continuing in block 590, the system waits for the new writernode to become the head of the queue. For example, the system may waitby spinning and if spinning fails to acquire the lock in a thresholdperiod or number of loop iterations, the system may block waiting for aprior lock holder to awaken the thread when the prior lock holderreleases the lock. The writer node becomes the head of the queue whenall prior nodes in the queue complete acquiring and subsequentlyreleasing the lock. After block 590, these steps conclude.

FIG. 6 is a flow diagram that illustrates processing of the system torelease a read or write lock, in one embodiment. Beginning in block 610,the system receives a request to release a previously acquired lock. Forexample, an application may call a function provided by the system forreleasing a lock. The system as described herein expects the applicationto manage balancing requests to acquire the lock with requests torelease the lock. However, those of ordinary skill in the art willrecognize that it is a common modification to locks to use thread localstorage or other techniques to allow unbalanced acquire and releaserequests with a locking scheme, and similar modifications can be appliedto the system described herein.

Continuing in decision block 620, if the release request is for a writernode, then the system jumps to block 650, else the system continues atblock 630. The system may distinguish a read lock release from a writelock release, for example, by providing separate functions for each ofthese two operations. Alternatively or additionally, the system maydetermine the appropriate type of release based on the type of nodecurrently at the head of the queue (and thus currently holding thelock).

Continuing in block 630 for a reader node, the system decrements areader count, as described further with reference to FIG. 7. Forexample, the system may acquire a queue lock and attempt to update anindexed lock count as described further herein. Continuing in decisionblock 640, if decrementing the reader count released the last readerassociated with the reader node, then the system continues at block 650,else the system completes. As described further herein, a single readernode may represent many readers, and can be released only when each ofthe associated readers have released the lock. Continuing in block 650,the system releases the next waiting node in the queue subsequent to thecurrent node. For example, the system may modify a variable thatspinning waiting nodes will notice on the next loop iteration, or thesystem may call an operating system provided function to wake threadsthat went to sleep blocking for the lock to be available. Releasing thenext waiting node may include removing the head node and make thewaiting node, if any, the new head of the queue. The next waiting nodeis waiting for the condition of becoming the head of the queue todetermine that the node's associated thread(s) hold the lock. The queuehead value is the variable that spinning waiting nodes test to determinethe lock status.

Continuing in block 660, the system deletes the current node that hasbeen released. For example, the system may deallocate the node or returnthe node to a pool of nodes for later reuse. Object allocation anddeallocation is often performance intensive, and particularly whereobjects are frequently created and destroyed can represent a large partof an application's overhead. Thus, using an object pool in conjunctionwith the system allows objects to be quickly reused, avoidingunnecessary setup and teardown of the objects. After block 660, thesesteps conclude.

FIG. 7 is a flow diagram that illustrates processing of the system torelease a reader, in one embodiment. Beginning in block 710, the systemdetermines a distribution index associated with a current thread. Thesystem attempts to distribute which of multiple lock counts each threadincrements to reduce cache contention produced by having a single lockcount. The thread identifier of each thread provides a semi-random bitof information that the system may use, based on a hash algorithm orother distribution scheme common in the art, to select an index amongmultiple available indices within an array of lock counts. Continuing inblock 720, the system locks the queue so that the system can safely readand update data members associated with the queue. There is a monitorlock associated with each lock count that is acquired in block 720. Thelock and lock count are in the same cache line, so that obtaining themonitor lock does not introduce cache contention.

Continuing in block 730, the system decrements the indexed reader countbased on the determined distribution index. The indexed reader count isin a cache line isolated from other reader counts to reduce cachecontention produced by the act of updating the reader count. Continuingin decision block 740, if the decrement made the indexed reader countzero, then the system continues at block 750, else the system jumps toblock 790. Continuing in decision block 750, if there is a writer nodesubsequent to the current reader node, then the system continues atblock 760, else the system jumps to block 790. A subsequent writer nodeindicates that the system has received a request to acquire the writelock. To ensure that readers do not starve writer requests (i.e.,fairness), the system prevents further reader requests on the samereader node. When the existing reader requests are finally released, thesystem may then wake the waiting writer.

Continuing in block 760, the system decrements a countdown of remainingreaders associated with the reader node. Continuing in decision block770, if the decrement made the countdown of remaining readers zero, thenthe system continues at block 780, else the system jumps to block 790.Continuing in block 780, the system sets information indicating that thelast reader associated with the reader node has released the lock. Thecode that invoked these steps uses this information to wake any waitingsubsequent node in the queue. Continuing in block 790, the systemunlocks the queue so that other waiting threads can modify the queue.The system may also remove the head of the queue to allow the next nodeto become the head, and thus acquire the lock. After block 790, thesesteps conclude.

In some embodiments, the scalable lock system leaves the last readernode at the head of the queue after each reader represented by the nodehas released the lock. Reader node structures can be resource intensiveto create and teardown, so if the last read lock is released with nopending write-lock requests, the system leaves the reader node in placeat the head of the queue, ready to handle the next read-lock request.

In some embodiments, the scalable lock system provides writer and/orreader fairness. Writer fairness refers to the ability of a writer toobtain the lock even though readers continue to request the lock whilesome readers already hold the lock. Reader fairness refers to theability of a reader to obtain the lock when a writer holds the lock andsubsequent writers are waiting to acquire the lock. The system preventsany lock acquisition request from blocking indefinitely. The system mayallow some amount of unfairness to increase throughput, but only abounded amount—the system assures a waiting request of eventuallyacquire the lock.

In some embodiments, the scalable lock system provides lockupgradeability. A common feature of existing reader/writer locks is theability to acquire a lock in an upgradeable mode—as a read lock that mayoptionally later be upgraded to a write lock, either through anadditional interface function or by calling a function for acquiring awrite lock on a thread that already has a read lock. Only one thread mayhold the lock in upgradeable mode at a time (though other threads mayhold the lock in read mode at the same time). To upgrade a lock, thesystem drains off any existing other readers to allow the current threadto upgrade to a write lock, potentially in advance of other writerequests enqueued after the initial read request of the threadrequesting the lock upgrade.

In some embodiments, the scalable lock system allows applications oradministrators to configure the size and/or number of lock count indexesto make tradeoffs between space efficiency and the level of cachecontention reduced by the system. In addition, the application may haveinformation about the cache configuration of the system, for example,that makes a particular configuration of lock count indices moreefficient.

In some embodiments, the scalable lock system dynamically determineswhether to use the techniques described herein versus other availablelocking techniques. For example, the space used for the paddingdescribed herein may be high enough to warrant choosing a simplecritical section for a sufficiently low number of processors. Thus, ifthe system observes that the number of processors or other attributes ofthe environment in which the system is used do not meet a threshold, thesystem may select other locking techniques that provide similar lockguarantees with more favorable performance tradeoffs. In addition, thesystem may perform steps to reduce memory space consumed by the locksystem and minimize padding (which is often large to push read countsinto separate cache lines). The system may, for example, allow multiplereader nodes on the queue to share a data structure of read lock counts.Each reader node may get a separate index in the counts to keep the dataof the reader nodes separate, but each reader node would not involve anew set of padding between counts, as the same padding would serve thepurpose of keeping the counts in separate cache lines.

Example Implementation

The following paragraphs present an example implementation of the systemdescribed herein. The following class illustrates a typical interfacefor a reader-writer lock.

class RWLock {  public RWLock( );  public ReadAcquire( );  publicReadRelease( );  public WriteAcquire( );  public WriteRelease( ); }

Code examples given herein use the C# programming language. However,those of ordinary skill in the art will recognize that similartechniques can be performed using various other programming languages toimplement the system. In the interface above, the acquire operationsblock until the lock is obtained in the requested mode. The releaseoperations do not block. This example assumes that applications properlymatch acquires with releases of the appropriate (read/write) lock.Implementations of this interface may provide debugging aids to detectincorrect use of this interface, but such aids are outside the scope ofthis example.

The basic lock data structure is queue-based. The queue is a linked listof AccessNode objects, connected via an m_next field of that type. EachAccessNode is either a WriterNode or a ReadersNode. A WriterNoderepresents one thread attempting to acquire the lock in write mode, anda ReadersNode represents a set of one or more threads attempting toacquire the lock in read mode. As shown in the following code, theRWLock type maintains pointers to the head and tail of the queue. Inresponse to new requests, the RWLock adds nodes at the tail, and thethread(s) associated with the node at the head hold the lock in the modeindicated by the type of the node.

class RWLock {  ...  private AccessNode m_qHead;  private AccessNodem_qTail; }

A WriterNode adds no new fields or methods to AccessNode. A ReadersNode,however, has a structure shown in the following code.

const int CacheLineBytes = 64; ... [StructLayout(LayoutKind.Sequential)]private struct ReadLockElemStruct {  public int m_lockCount;  // Padwith enough bytes to avoid false sharing.  private fixed byte  m_paddingAfter[CacheLineBytes − sizeof(int)]; } private classReadLockElem {  public ReadLockElemStruct m_s; } private classReadersNode: AccessNode {  private ReadLockElem[ ] m_arr;  private intm_countDown;  public int m_idx;  ... }

This code utilizes several features of C# and .NET that may not befamiliar. The notation “[StructLayout(LayoutKind.Sequential)]” indicatesthat the struct type being defined has its fields laid out in the orderindicated. Without this constraint, the runtime system is free to layout fields in any order. The “fixed” notation indicates an array laidout “inline” in the struct. The result is a struct with one int field,then enough padding bytes to fill out a cache line. Although 64 bytes isshown as the size of the cache line in the example code, this size mayvary by implementation and processor hardware used. In C# and Microsoft.NET, objects (e.g., instances of class types) contain implicit monitorlocks, but struct types do not. This example assumes lockable instancesof this struct type, so the ReadLockElem class is created, which has onefield of this struct type. In the common case, .NET monitor locking isaccomplished with reads and writes to a “header word” at the start of anobject. This example relies on one more feature of the Microsoft .NETimplementation: if a single thread allocates several ReadLockEleminstances consecutively, they are likely to be allocated contiguously inmemory (and if they are not, they are highly likely to be allocated asonly two contiguous sequences, with a somewhat random gap between). TheReadersNode type contains an array of ReadLockElem instances initializedwith consecutively allocated instances. In each of these structures, thefunctions described herein are likely to access the object header wordand the m_lockCount field, but the padding bytes ensure that in acontiguous sequence of ReadLockElem instances, these words will be ondifferent cache lines for each object. The functions of the m_countDownand m_idx fields will be explained further herein.

The example implementation obeys the following rules to manageconcurrency. First, a thread can only modify the m_qHead and m_qTailfields of an instance of the RWLock class when it holds the monitor lockon that instance. Second, the m_lockCount field of a ReadLockEleminstance can only be accessed by a thread that holds the monitor lock onthat instance. Third, the m_next field of an AccessNode is null when thenode is allocated, and makes a single transition to a non-null value inits lifetime. This transition is only performed by threads that hold themonitor lock on the top-level RWLock. In addition, for a givenReadersNode instance the system ensures that the thread causing thistransition hold the monitor locks on all the ReadLockElem instancespointed to by the m_arr member of the given instance. Finally, them_countDown member of a ReadersNode instance is zero when the object isallocated. After that, m_countDown may make a single transition fromzero to a non-zero value; as with the m_next field's null-to-non-nulltransition, only a thread that holds the locks on all the ReadLockElemsof the instance performs the transition. After the transition to anon-zero value, threads may decrement the value back towards zero. Thesystem uses atomic hardware instructions (in a loop) for thesedecrements. The use of these rules to justify concurrency safety ispointed out further herein.

The first method described is ReadAcquire. Following is example code forimplementing this method as discussed further herein.

public void ReadAcquire( ) {  Thread curThrd = Thread.Current;  while(true) {   AccessNode an = m_qTail;   if (an != null && an isReadersNode) {    ReadersNode rn = (ReadersNode)an;    if(!rn.IncLockCountForThread(curThrd))     continue;    WaitUntilHead(rn);  } else {    ReadersNode rn = AllocReadersNode( );   rn.IncLockCountForThread(curThrd);    lock (this) {     an = m_qTail;    if (an == null) {      m_qHead = m_qTail = rn; return;     } else if(an is ReadersNode) {      rn.DecLockCountForThread(curThrd);     DeallocReadersNode(rn);      continue; // try again.     } else {// an is a writer node.      an.m_next = rn;      m_qTail = rn;     }   }    WaitUntilHead(rn);   }   return;  } }

As shown, acquiring the read lock includes a loop, since lock-freetechniques may include retrying several times. In the loop, the functionreads (with no locking) the current queue tail. If the tail is anon-null ReadersNode, the current thread will attempt to increment thelock count for its element. The danger of using the “dirty read” is thatthe node may be deleted from the queue between that read and theoperations done on the node. The IncLockCountForThread operation handlesthat situation, as will be discussed, and returns a Boolean valueindicating whether the increment was successful. If the return value isfalse, the ReadersNode was closed to further read locking by asubsequent write-locking thread. In this case, the function retries theloop. If the increment operation is successful, then the thread waitsfor the tail node in which it incremented a lock count to become thehead node of the queue (the WaitUntilHead operation is described in moredetail below).

If the observed tail node of the queue is not a ReadersNode, then thefunction allocates a new readers node to append to the queue. This nodewill have null m_next, zero m_countDown, and an m_arr field initializedto an array containing pointers to (contiguously allocated) ReadLockEleminstances with lock count zero. No thread on the new node or itssub-objects will hold any monitor locks. The system increments the lockcount for the current thread in this ReadersNode—with no locking, sincethe newly allocated node is still thread-local. Next, the function locksthe RWLock, since it is going to modify the queue structure. Thefunction then reads the m_qTail field while holding the lock. If resultis null, the queue is empty, so the function creates a one-elementqueue. The new ReadersNode is at the head of the queue, so the operationcan return, since the current thread has acquired a read lock.Otherwise, if the locked read of the tail determines the tail now to bea ReadersNode, the function undoes the lock count increment, deallocatesthe node (the system may use an “object pool” to speed allocation ofReadersNode instances), and re-executes the loop. Finally, if theobserved head is non-null and is not a ReadersNode, it is a WriterNode,so the function sets the m_next field of that tail node to the newReadersNode (recall that the m_next field of a WriterNode may bemodified while holding the lock on the entire RWLock, which is true atthis point), and updates m_qTail (which is also justified by the lockstatement on the RWLock).

Following are example implementations of the IncLockCountForThread andWaitUntilHead helper functions used in the discussion above.

// Helper functions public bool IncLockCountForThread(Thread t) {  intid = t.GetHashCode( );  int ind = id % NProcs;  ReadLockElem rle =m_arr[ind];  lock (rle) {   // If we have a waiting writer, can'tincrement.   if (m_next != null) return false;   // Otherwise...  rle.m_s.m_lockCount++   return true;  } } private voidWaitUntilHead(AccessNode an) {  for (int k = 0; k < SpinCount; k++) {  if (an == m_qHead) return;   Spin(1);  }  lock (an) {   while (an !=m_qHead) {    Monitor.Wait(an);   }  } }

IncLockCountForThread starts by obtaining the locking thread's hashcode, which is distributed evenly over the 32-bit integers, and using amodulus operation to get an index in the interval [0::NProc), whereNProc is the size of a ReadersNode instance's array of ReadLockElemstructures. The function then reads the given array element, obtainingthe proper ReadLockElem instance for the locking thread. The functionlocks this ReadLockElem instance and checks whether the ReadersNodeinstance's m_next field is non-null. If the m_next field is non-null,then a subsequent WriterNode already enqueued, and the currentReadersNode is closed to further read locking; the function returnsfalse to indicate this condition. Otherwise, m_next is null, and willremain so while the lock is held (relying on the rule that all suchlocks are acquired in order to change the m next field of aReadersNode). The function can therefore increment m_lockCount toincrease the total number of read-locking threads (access to m_lockCountis justified by holding the lock on the containing ReadLockElem), andreturn true to indicate success.

The WaitUntilHead function starts with some amount of spin waiting. Thisloop will perform some number of iterations, each of which will performa dirty (unlocked) read of the RWLock's m_qHead field, and test whetherthe current thread's node is at the head. If so the function returns; ifnot, the function calls Spin to do some amount of busy work, and endsthe loop. Performing the dirty read of m_qHead helps performance: if alock were forced to observe this, the locking would create a contendedcache line. Fortunately, it is safe to do the dirty read here, since thepredicate we are waiting to observe is “stable for current thread:” ifit becomes true, it can only be falsified by an action of the currentthread. In this case, if the current thread's AccessNode becomes thehead of the queue, then it will remain at the head of the queue at leastuntil the current thread releases its lock.

With proper tuning of the SpinCount parameter and the amount of busywork done in the Spin routine, the function can acquire many lockswithout blocking. Of course, spinning too much can lead to excessivewasted work, especially when there are more runnable threads thanprocessors. These tradeoffs are standards with locks that balance spinwaiting and blocking, and are familiar to those of ordinary skill in theart. When spin waiting fails, the function starts a standard monitorwait loop. The function locks the current thread's AccessNode, and thenenters a loop that waits for the AccessNode to become the queue head.Again, the read here of m_qHead is dirty, as before, but is justified bythe same observation. The Monitor.Wait(an) operation releases themonitor lock on the AccessNode and waits for some other thread to do aPulse or PulseAll operation on the AccessNode (the Microsoft .NETplatform calls this Pulse; other platforms use the name Notify). Thesystem is careful that all operations that change the queue head performthis wakeup operation.

We next describe the method for acquiring a write lock; considering theoperations in this order will make certain aspects of releasing a readlock clearer. The following code illustrates one implementation of afunction for acquiring the write lock.

public void WriteAcquire( ) {  WriterNode wn = new WriterNode( );  lock(this) {   AccessNode an = m_qTail;   if (an == null) {    m_qHead =m_qTail = wn; return;   } else if (an is WriterNode) {    an.m_next =wn;    m_qTail = wn;   } else {    ReadersNode rn = (ReadersNode)an;   if (rn.InstallWriterSuccessor(wn)) {     m_qHead = m_qTail = wn;    DeallocReadersNode(rn);     return;    } else {     m_qTail = wn;   }   }  }  WaitUntilHead(wn); }

The function first allocates a new WriterNode. In contrast withReadAcquire, WriteAcquire immediately locks the RWLock and reads thequeue tail. If the queue tail is null, the function sets up asingle-node queue and returns, indicating that the current thread ownsthe write lock. Otherwise, if the non-null tail node is a WriterNode,the function makes the new WriterNode the new tail, and falls through torelease the lock on the RWLock and waits until the new WriterNode is thequeue head. If these tests fail, then the tail node is a ReadersNode. Inthis case, the function calls InstallWriterSuccessor to close theReadersNode to further read locking, make the argument node WriterNodethe successor of the ReadersNode, and return a Boolean value indicatingwhether the total lock count of the ReadersNode was zero. This can onlybe true if the ReadersNode is at the head of the queue: in ReadAcquireevery ReadersNode has a total lock count greater than zero when itbecomes part of the queue, and the total lock count is only decrementedby lock releases, which happen only after acquires return, which happensonly when the corresponding node becomes the queue head. Therefore, ifInstallWriterSuccessor returns true, WriteAcquire can create asingle-node queue, deallocate the former queue head, and returnimmediately indicating that the current thread has acquired the lock inwrite mode. Otherwise, if InstallWriterSuccessor returns false,WriteAcquire completes the queue append by making the WriterNode the newtail of the queue, and again falls through to release the lock and waitfor the WriterNode to become the queue head.

Following is an example implementation of the InstallWriterSuccessorhelper function of the ReadersNode class used in the discussion above.

public bool InstallWriterSuccessor(WriterNode wn) {  int nonZeroLCs = 0; for (int i = 0; i < NProcs; i++) {   Monitor.Enter(m_arr[i]);   if(m_arr[i].m_s.m_lockCount > 0) nonZeroLCs++;  }  m_countDown =nonZeroLCs;  m_next = wn;  for (int i = 0; i < NProcs; i++) {  Monitor.Exit(m_arr[i]);  }  return (nonZeroLCs == 0); }

InstallWriterSuccessor initializes a variable (nonZeroLCs) that willcount the number of ReadLockElem instances with non-zero lock counts.Next, a loop acquires the monitor locks on all the ReadLockEleminstances in the ReadersNode (in ascending index order, which preventsdeadlocks if multiple threads execute this method concurrently). Afterthe monitor lock has been acquired, the function reads the lock count(obeying the locking discipline for accessing the lock count), and if itis non-zero, increments the nonZeroLCs variable. When all the locks havebeen acquired, the function initializes the m_countDown and m_nextfields to possibly non-zero and non-null values. Note that this codeobeys the locking discipline described for these transitions since allthe ReadLockElem monitor locks for the ReadersNode are held. After this,the subsequent loop releases all the ReadLockElem monitor locks (orderis irrelevant here), and then the method returns a Boolean indicatingwhether the number of non-zero lock counts observed was zero.

The following code shows an example implementation of the ReadReleasefunction for releasing a read lock held by a thread.

public void ReadRelease( ) {  ReadersNode rn = (ReadersNode) m_qHead; if (rn.DecLockCountForThread(Thread.CurrentThread)) {   ReleaseHead( );  DeallocReadersNode(rn);  } } public bool DecLockCountForThread(Threadt) {  int id = t.GetHashCode( );  int ind = id % NProcs;  ReadLockElemrle = m_arr[ind];  lock (rle) {   int lc = (−−rle.m_s.m_lockCount);   if(lc == 0) {    if (m_next != null) {     if (Interlocked.Decrement(refm_countDown) == 0)      return true;    }   }  }  // Otherwise... return false; }

The discussion herein has assumed that releases are balanced, so that athread executing ReadRelease has last executed a successful ReadAcquireoperation on the RWLock. Therefore, ReadRelease may assume that the headnode of the lock's queue is a ReadersNode whose total lock countincludes a contribution from the current thread, and is thereforenon-zero. The DecLockCountForThread function finds the array index forthe decrementing thread, reads the corresponding ReadLockElem instance,and locks the ReadLockElem. DecLockCountForThread decrements the lockcount of the ReadLockElem instance and checks whether the new lock countis zero. If the new lock count is zero, then the function checks whetherthe ReadersNode has a non-null m_next field. If the m_next field isnon-null, then a previous execution of InstallWriterSuccessor hasinitialized m_next and m_countDown, thus closing this node from furtherread locking. The return value of the DecLockCountForThread function istrue only if the ReadersNode on which it is executed has a WriterNodesuccessor, and its total lock count is now zero, so the successor canbecome the new queue head. Therefore, the function uses an atomichardware instruction to decrement m_countDown, and if the post-decrementvalue returned is zero, the function returns true. Otherwise, thefunction returns false.

In ReadRelease, if the call to DecLockCountForThread returns true,ReadRelease calls ReleaseHead to remove the head node from the queue.This general method calls PulseAll on the new queue head node, if thereis one. Finally, ReadRelease deallocates the removed node (e.g., as partof an object-pooling scheme for ReadersNode instances).

The WriteRelease method is not shown, since it only includes a call tothe same ReleaseHead method called by ReadRelease. The details ofReleaseHead impact performance as described further herein. Thefollowing code is an example implementation of the ReleaseHead function.

private void ReleaseHead( ) {  AccessNode newHead = m_qHead.m_next;  if(newHead == null) {   lock (this) {    if (m_qTail == m_qHead) {    m_qHead = null; m_qTail = null;     return;    }   }  }  newHead =m_qHead.m_next;  m_qHead = newHead;  lock (newHead) {  Monitor.PulseAll(newHead);  } }

Note that only the thread that completes the release of the lock(s)corresponding to the queue head calls ReleaseHead—no other thread cancall this method until ReleaseHead changes the queue head. Thisjustifies the unlocked read of m_qHead in ReleaseHead, since only thecurrent thread can change m_qHead. The m_next field of the queue head isalso read without locking. By the concurrency rules described herein, ifthe test of m_next determines that the next pointer is non-null, itsvalue is stable and can be read without locking. If m_next is null,however, m_next may change to a non-null value if another threadenqueues a new node after m_next. Thus, ReleaseHead acquires the monitorlock on the RWLock in this case, which it holds in order to perform thistransition by the concurrency control rules. ReleaseHead then performs a“double-check” idiom, reading the queue head and tail again whileholding the lock to verify that the queue has a single element. If so,then the method resets the queue to the empty state, and the methodreturns. If this test fails, the queue had more than one element, so thefunction falls through and releases the lock—when the successor of thehead is then read it is guaranteed to be non-null. The function thenupdates the queue head. Immediately after this write, other threadsdoing spin waiting may observe that their node is at the head of thequeue, and determine that they therefore own the lock. However, thecurrent thread has already identified the newHead queue node whosethread(s) it is responsible for waking if they have blocked, so itpulses those objects.

From the foregoing, it will be appreciated that specific embodiments ofthe scalable lock system have been described herein for purposes ofillustration, but that various modifications may be made withoutdeviating from the spirit and scope of the invention. Accordingly, theinvention is not limited except as by the appended claims.

1. A computer-implemented method for acquiring a read lock in a multipleprocessor system, the method comprising: receiving a request to acquirea read lock; inspecting a queue of waiting reader and writer nodes toidentify a current queue tail, wherein a node at the head of the queuerepresents one or more threads that currently hold the lock and whereinnew requests to acquire the lock are added at the current queue tail;upon determining that the current queue tail is a reader node,incrementing a reader count by performing the following: determining adistribution index associated with a current thread, wherein thedistribution index distributes received read lock requests to multiplelock counts to reduce cache contention versus a single lock count;incrementing an indexed reader count based on the determineddistribution index; and waiting for the reader node to become the headof the queue, and in response to the reader node becoming the head ofthe queue responding to the request indicating the lock is acquired,wherein the preceding steps are performed by at least one processor. 2.The method of claim 1 wherein determining a distribution index comprisesperforming a hash function on a thread identifier of a current threadand determining a modulus of a hash result based on a number of lockcounts.
 3. The method of claim 1 further comprising, before incrementingthe indexed reader count, determining whether the queue contains awriter node subsequent to the current reader node, and in response todetermining that the queue contains a subsequent writer node, retryingthe read lock request to ensure that readers do not starve writerrequests.
 4. The method of claim 1 wherein the indexed reader count isin a cache line isolated from other reader counts to reduce cachecontention produced by the act of updating the reader count.
 5. Themethod of claim 1 wherein the indexed reader count is offset from otherreader counts by an amount of padding equal to or greater than a cacheline width of a processor of the multiple processor system.
 6. Themethod of claim 1 wherein waiting for the reader node to become the headof the queue comprises spinning for a threshold period and if the readernode does not become the head of the queue during the threshold periodblocking waiting for a prior lock holder to release the lock.
 7. Acomputer system for providing a reader/writer lock having favorablecache contention characteristics, the system comprising: a processor andmemory configured to execute software instructions; an interfacecomponent configured to provide an interface to application code andreceive requests to acquire and release locks in both a read mode and awrite mode; a queue component configured to maintain a list of readerand writer nodes that represent requests to acquire a lock and anycurrent holder of the lock; a reader state component configured tomaintain an indexed list of reader lock counts for one or more readernodes in the list maintained by the queue component, wherein the indexedlist is structured so that lock counts are distributed with enough spacebetween each count so that accessing a lock count at one index locationis associated with a different cache line than accessing a lock count atany other index location; a blocking component configured to allowthreads to block waiting for the lock efficiently and wake threads inresponse to a particular thread releasing the lock; and a nodeallocation component configured to allocate new reader and writer nodesfor inclusion on the list maintained by the queue component.
 8. Thesystem of claim 7 wherein the interface component is further configuredto provide a programmatic interface to applications through Microsoft.NET that includes a class with one or more functions that anapplication can invoke to acquire and release read and write locks. 9.The system of claim 7 wherein the queue component is further configuredto store a node associated with the current holder of the lock at thehead of the list and wherein each subsequent node in the list representsa next in line request to receive the lock after the current holder ofthe lock releases the lock.
 10. The system of claim 7 wherein the queuecomponent is further configured to provide lock fairness by enforcingacquisition of the lock based on an order of received requests to ensurethat one type of request will not prevent another type of request fromacquiring the lock.
 11. The system of claim 7 wherein the reader statecomponent prevents cache contention in response to a thread running onone processor modifying a lock count because of acquiring or releasing aread lock.
 12. The system of claim 7 wherein the reader state componentdistributes the lock counts according to a scheme based on a number ofprocessors associated with the system.
 13. The system of claim 7 whereinthe reader state component distributes the lock counts according to ascheme based on a number of threads using the lock.
 14. The system ofclaim 7 wherein the blocking component is further configured to spin fora threshold number of loops before putting a thread to sleep to beawoken when the lock is available and wherein in response to the lockbeing released by a current lock holder, the blocking component wakes athread of the next node in the list maintained by the queue componentindicating that the thread has the lock.
 15. The system of claim 7wherein the node allocation component is further configured to useobject pooling to provide efficient creation and destruction of nodes.16. A computer-readable storage medium comprising instructions forcontrolling a computer system to release a read/write lock, wherein theinstructions, when executed, cause a processor to perform actionscomprising: receiving a request to release a previously acquired readlock; determining a distribution index associated with a current thread;decrementing an indexed reader count based on the determineddistribution index, wherein the indexed reader count is in a cache lineisolated from other reader counts to reduce cache contention produced byupdating the indexed reader count; determining whether there is a writernode subsequent to a current reader node associated with the previouslyacquired read lock; determining that a total of all indexed readercounts associated with the current reader node is zero; and in responseto determining that there is a writer node subsequent and that the totalof all indexed reader counts is zero, releasing the next waiting node inthe queue subsequent to the current reader node.
 17. The medium of claim16 wherein determining a distribution index comprises identifying adistribution index determined when the read lock was previously acquiredto increment the lock count.
 18. The medium of claim 16 whereinreleasing the next waiting node comprises modifying a variable thatspinning waiting nodes will notice on the next spin loop iteration. 19.The medium of claim 16 wherein releasing the next waiting node comprisescalling an operating system provided function to wake threads that wentto sleep blocking for the lock to be available.